package io.chain.ko.controller;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.chain.ko.ajax.AjaxResult;
import io.chain.ko.dto.TUserDTO;
import io.chain.ko.entity.TUser;
import io.chain.ko.service.UserService;
import io.chain.ko.util.CopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user")
@Api("用户接口")
public class UserController {
	@Autowired
	private UserService userService;
	
	@PostMapping("/saveUser")
	@ApiOperation("保存用户")
	public AjaxResult<TUserDTO> saveUser(@RequestBody TUserDTO tuserDTO) {
		AjaxResult<TUserDTO> ajaxResult=new AjaxResult<>();
		TUser tuser=new TUser();
		BeanUtils.copyProperties(tuserDTO, tuser);
		userService.saveUser(tuser);
		BeanUtils.copyProperties(tuser, tuserDTO);
		ajaxResult.setStatus(200L);
		ajaxResult.setData(tuserDTO);
		return ajaxResult;
	}
	
	@PostMapping("/findUser/{pageSize}/{page}")
	@ApiOperation("分页查找用户")
	public AjaxResult<List<TUserDTO>> findUser(@PathVariable Integer pageSize, @PathVariable Integer page,@RequestBody TUserDTO tuserDTO) {
		AjaxResult<List<TUserDTO>> ajaxResult=new AjaxResult<>();
		TUser tuser=new TUser();
		BeanUtils.copyProperties(tuserDTO, tuser);
		List<TUser> entityList=userService.findUser(pageSize, page, tuser);
		List<TUserDTO> dtoList=CopyUtils.copy(entityList, TUserDTO.class);
		ajaxResult.setStatus(200L);
		ajaxResult.setData(dtoList);
		return ajaxResult;
	}
}
